//
//  JProductCommentView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/29.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JProductCommentView.h"

#import "JCommentView.h"

@implementation JProductCommentView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpView];
        
    }
    return self;
}

- (void)setUpView
{
    _titleLabel = [YYLabel new];
    _titleLabel.top = 0;
    _titleLabel.left = 20;
    _titleLabel.size = CGSizeMake(kWindowW - 40, 40);
    _titleLabel.font = kFONT_TITLE(16);
    _titleLabel.textColor = kRGBColor(78, 78, 78);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.numberOfLines = 0;
    _titleLabel.text = @"宝贝评论";
    [self addSubview:_titleLabel];
    
    CALayer *line1 = [CALayer layer];
    line1.contents = (__bridge id _Nullable)([UIImage imageNamed:@"pzh"].CGImage);
    line1.centerY = _titleLabel.centerY;
    line1.left = _titleLabel.width/2.0 - 80 ;
    line1.size = CGSizeMake(40, 1);
    [self.layer addSublayer:line1];
    
    CALayer *line2 = [CALayer layer];
    line2.contents = (__bridge id _Nullable)([UIImage imageNamed:@"pzh"].CGImage);
    line2.centerY = _titleLabel.centerY;
    line2.left = _titleLabel.width/2.0 + 80 ;
    line2.size = CGSizeMake(40, 1);
    [self.layer addSublayer:line2];
    
    
    _CommentLabel = [YYLabel new];
    _CommentLabel.top = _titleLabel.bottom;
    _CommentLabel.left = 20;
    _CommentLabel.size = CGSizeMake(kWindowW - 40, 40);
    _CommentLabel.font = kFONT_TITLE(16);
    _CommentLabel.textColor = kRGBColor(78, 78, 78);
    _CommentLabel.textAlignment = NSTextAlignmentCenter;
    _CommentLabel.numberOfLines = 0;
    _CommentLabel.text = @"暂无评论";
    [self addSubview:_CommentLabel];
    
    _bgView = [[JCommentView alloc]init];
    _bgView.frame = CGRectMake(0, 40, kWindowW, [_bgView getViewHeight]);
    [self addSubview:_bgView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.top = _bgView.bottom;
    btn.centerX = _bgView.centerX - 60;
    btn.size = CGSizeMake(120, 40);
    [btn setTitle:@"查看全部评论" forState:UIControlStateNormal];
    btn.titleLabel.font = kFONT_TITLE(16);
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.layer.borderWidth = 0.5;
    btn.layer.borderColor = [UIColor redColor].CGColor;
    [btn addTarget:self action:@selector(inputStoreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    _btn = btn;
}

- (void)inputStoreBtnClick:(UIButton *)btn
{
    if (_AllCommentBlock) {
        _AllCommentBlock();
    }
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    if (_dataArr.count == 0) {
        _CommentLabel.hidden = NO;
        _bgView.hidden = YES;
    }
    else
    {
        _bgView.hidden = NO;
        _CommentLabel.hidden = YES;
        _bgView.commentModel = dataArr[0];
        CGFloat height = [_bgView getViewHeight];
        _bgView.frame = CGRectMake(0, 40, kWindowW, height);
        _btn.top = _bgView.bottom;
        _btn.centerX =  _titleLabel.centerX;
        _btn.size = CGSizeMake(120, 40);
    }
}

- (CGFloat)getViewHeight
{
    if (_dataArr.count == 0)
    {
        return 80;
    }
    else
    {
        CGFloat height = _titleLabel.height + [_bgView getViewHeight] + _btn.height + 20;
        return height;
    }
    return 0;
    
}

@end
